import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'package:wowhy/widgets/my_refresh_header.dart';
import 'package:wowhy/widgets/my_refresh_footer.dart';

import '../admin_user_list_page/action.dart';

import 'action.dart';
import 'state.dart';

Widget buildView(
  AdminUserListState state,
  Dispatch dispatch,
  ViewService viewService,
) {
  return Scaffold(
    appBar: AppBar(
      title: Text('管理员列表'),
      actions: [
        IconButton(
          icon: Icon(Icons.add),
          onPressed: () {
            dispatch(AdminUserListActionCreator.onAdd());
          },
        ),
        IconButton(
          icon: Icon(Icons.search),
          onPressed: () {
            dispatch(AdminUserListActionCreator.onSearch());
          },
        )
      ],
    ),
    body: buildListView(state, dispatch, viewService),
  );
}

Widget buildListView(
  AdminUserListState state,
  Dispatch dispatch,
  ViewService viewService,
) {
  final ListAdapter adapter = viewService.buildAdapter();

  return SmartRefresher(
    controller: state.refreshController,
    enablePullUp: true,
    enablePullDown: true,
    header: MyRefreshHeader(),
    footer: MyRefreshFooter(),
    onRefresh: () => dispatch(AdminUserListActionCreator.onRefresh()),
    onLoading: () => dispatch(AdminUserListActionCreator.onFetch()),
    child: state.noData
        ? buildEmptyView(state, dispatch, viewService)
        : ListView.builder(
            physics: ClampingScrollPhysics(),
            itemBuilder: adapter.itemBuilder,
            itemCount: adapter.itemCount,
          ),
  );
}

Widget buildEmptyView(
  AdminUserListState state,
  Dispatch dispatch,
  ViewService viewService,
) {
  return Center(child: Text('当前没有数据'));
}
